package com.examination.mlib.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;

import androidx.annotation.Nullable;

import java.io.File;
import java.io.IOException;

public class MediaControl {
    private MediaPlayer mediaPlayer;

    private MediaControl() {
    }

    private volatile static MediaControl mediaControl = null;

    public static MediaControl getInstance() {
        if (mediaControl == null) {
            synchronized (MediaControl.class) {
                if (mediaControl == null) {
                    mediaControl = new MediaControl();
                }
            }
        }
        return mediaControl;
    }

    public MediaPlayer getMediaPlayer() {
        return mediaPlayer;
    }

    /**
     * 播放
     *
     * @param context context
     *                //     * @param rawRes  RawRes
     */
    public void play(Context context, String path, @Nullable PlayListener listener) {
        File sourceFile = new File(path);
        if (!sourceFile.exists()) {
            return;
        }
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            stop();
        }
        mediaPlayer = null;
        mediaPlayer = MediaPlayer.create(context, Uri.fromFile(sourceFile));
        if (mediaPlayer == null) {
            return;
        }
        mediaPlayer.setLooping(false);
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (mediaPlayer == null) {
                    return;
                }
                mediaPlayer.start();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (mediaPlayer == null) {
                    return;
                }
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
                if (listener != null) {
                    listener.onComplete();
                }
//                stop();
//                destroy();
            }
        });
    }


    /**
     * 播放
     *
     * @param context context
     *                //     * @param rawRes  RawRes
     */
    public void playAssets(Context context, String name, @Nullable PlayListener listener) {


        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            stop();
        }
        mediaPlayer = null;
        try {
            AssetFileDescriptor descriptor = context.getAssets().openFd(name);
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (mediaPlayer == null) {
            return;
        }
        mediaPlayer.setLooping(false);
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (mediaPlayer == null) {
                    return;
                }
                mediaPlayer.start();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (mediaPlayer == null) {
                    return;
                }
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
                if (listener != null) {
                    listener.onComplete();
                }
//                stop();
//                destroy();
            }
        });
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止播放
     */
    public void stop() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    /**
     * 销毁
     */
    public void destroy() {
        mediaControl = null;
        if (mediaPlayer != null) {
            stop();
        }
    }

    public interface PlayListener {
        void onComplete();
    }
}
